$(document).ready(function() {
  $(".normalLogNav").on("click", function() {
    $(this)
      .addClass("active")
      .siblings()
      .removeClass("active");
    $(".normalLog").removeClass("none");
    $(".quickLog").addClass("none");
  });
  $(".quickLogNav").on("click", function() {
    $(this)
      .addClass("active")
      .siblings()
      .removeClass("active");
    $(".normalLog").addClass("none");
    $(".quickLog").removeClass("none");
  });
  $("#nameOrPhone").blur(function() {
    let nameOrPhone = $("#nameOrPhone").val();
    if (nameOrPhone !== "") {
      $(".userCont").css("border", "1px solid #5944C3");
      $(".submit").css("background", "#5944C3");
    } else {
      $(".userCont").css("border", "1px solid rgba(0, 0, 0, .2)");
      $(".submit").css("background", "rgba(0, 0, 0, .2)");
    }
  });
  $("#CellPhoneNum").blur(function() {
    var CellPhoneNum = $("#CellPhoneNum").val();
    if (CellPhoneNum !== "") {
      $(".cellphoneCont").css("border", "1px solid #5944C3");
      $(".submit").css("background", "#5944C3");
    } else {
      $(".cellphoneCont").css("border", "1px solid rgba(0, 0, 0, .2)");
      $(".submit").css("background", "rgba(0, 0, 0, .35)");
    }
  });
  $(".toreg").on("click", function() {
    window.location.href = "register.html";
  });
  $("#captcha").on("click", getCaptcha);

  $("#normalLoginSubmit").on("click", function() {
    let nameOrPhone = $("#nameOrPhone").val(); // 获取手机号input的值
    let password = $("#passwordLog").val();
    if (nameOrPhone == "") {
      $(".modal-body").html("请输入用户名或手机号!");
      $(".modal").modal("show");
      return false;
    }
    ajaxXHR(
      "post",
      url + "v1/account/login",
      function(data) {
        localStorage.APISHotelname = data.data.account.name;
        localStorage.APISHoteltoken = data.data.account.token;
        localStorage.APISHotelid = data.data.account.id;
        localStorage.APISHotelavatar = data.data.account.avatar;
        $(".modal-body").html('登陆成功!<a href="index.html">去首页</a>');
        $(".modal").modal("show");
      },
      {
        nameOrPhone,
        password
      }
    );
  });
  $("#getLogVerfCode").on("click", function() {
    getRegVerfCode("login");
  });

  $("#quickLoginSubmit").on("click", function() {
    let phone = $(".cellPhoneNum").val(); // 获取手机号input的值
    let smsCaptcha = $("#verfLogCode").val(); // 获取短信验证码input的值
    if (nameOrPhone == "") {
      $(".modal-body").html("手机号不能为空!");
      $(".modal").modal("show");
      return;
    } else if (!/^1[3|4|5|7|8]\d{9}$/.test(phone)) {
      $(".modal-body").html("不是完整的11位手机号或者正确的手机号前七位!");
      $(".modal").modal("show");
      return;
    }
    ajaxXHR(
      "post",
      url + "v1/account/login/fast",
      function(data) {
        localStorage.name = data.data.account.name;
        localStorage.usertoken = data.data.account.token;
        localStorage.id = data.data.account.id;
        localStorage.captcha = smsCaptcha;
        $(".modal-body").html('登陆成功!<a href="index.html">去首页</a>');
        $(".modal").modal("show");
      },
      {
        phone: phone,
        smsCaptcha: smsCaptcha
      }
    );
  });
  $(".verfCode").on("keypress", function() {
    if (event.keyCode == "13") {
      event.preventDefault();
      //回车执行查询
      $("#quickLoginSubmit").click();
    }
  });
  $(".passwordLog").on("keypress", function() {
    if (event.keyCode == "13") {
      event.preventDefault();
      //回车执行查询
      $("#normalLoginSubmit").click();
    }
  });
});
